package MapperReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author caoxiaofeng
 * @date 2021-07-13 23:22
 */
public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration conf = new Configuration();

        // 1. 获取Job对象
        Job job = Job.getInstance(conf);

        // 2. 设置jar存储对象
        job.setJarByClass(WordCountDriver.class);

        // 3. 关联map与Reduce 类
         job.setMapperClass(WordCountMapper.class);
         job.setReducerClass(WordCountReducer.class);

         //4. 设置mapper阶段输出数据的key和value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

         // 5. 设置最终数据输出的key 和 value 类型
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

         // 6. 设置输入路径和输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //7. 提交job
//        job.submit();
        boolean b = job.waitForCompletion(true);
        System.out.println("job exec: "+b);
    }
}
